package com.project.yunshitong.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Environment;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Creat For: 11643
 * CreatTime: 2022/11/24
 * Description:
 */


public class FileSaveRFID {
    private Context mContext;
    @SuppressLint("SimpleDateFormat")
    SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd hh:mm:ss");
    SimpleDateFormat sdf1 = new SimpleDateFormat("YYYY-MM-dd");

    public FileSaveRFID(Context mContext) {
        this.mContext = mContext;
    }

    public static void deleteFilesOlderThanNDays(String directoryPath, int days) {
        File directory = new File(directoryPath);

        long now = System.currentTimeMillis();
        long threshold = now - days * 24 * 60 * 60 * 1000;
        File[] files = directory.listFiles();

        if (files != null) {
            for (File file : files) {
                if (file.lastModified() < threshold) {
                    file.delete();
                }
            }
        }
    }


    /**
     * 定义文件保存的方法，写入到文件中，所以是输出流
     */
    public void save(String Msg, String FileName) {

        new Thread(new Runnable() {
            @Override
            public void run() {

                // 指定文件目录
                String path = Environment.getExternalStorageDirectory().getPath();
                // 删除3天前的文件
                deleteFilesOlderThanNDays(path, 7);

                BufferedWriter out = null;
                File file = new File(Environment.getExternalStorageDirectory(), getFileName(FileName));
                try {
                    out = new BufferedWriter(new OutputStreamWriter(
                            new FileOutputStream(file, true), Charset.forName("gbk")));
                    out.write(Msg + sdf.format(System.currentTimeMillis()));
                    out.write("\r\n");//写入换行
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    /**
     * 定义文件读取的方法
     */
    public String read(String filename) throws IOException {
        FileInputStream fis = mContext.openFileInput(filename);
        byte[] buff = new byte[1024];
        StringBuilder sb = new StringBuilder("");
        int len = 0;
        while ((len = fis.read(buff)) > 0) {
            sb.append(new String(buff, 0, len));
        }
        fis.close();
        return sb.toString();
    }

    /**
     * get file name such as 20171031.txt
     *
     * @return
     */
    private String getFileName(String FileName) {
        ///获取当前读卡时间
        Calendar c = Calendar.getInstance();
        String date = sdf1.format(c.getTime());

        return date + FileName + ".txt";
    }

}
